package com.leetcode.LC_MED;

public class LC64 {
    public int minPathSum(int[][] grid) {

        if (grid == null || grid.length == 0) {
            return 0;
        }

        int[][] ints = new int[grid.length][grid[0].length];

        ints[0][0] = grid[0][0];

        //列
        for (int i = 1; i < grid.length; i++) {
            ints[i][0] = ints[i - 1][0] + grid[i][0];
        }

        //行
        for (int j = 1; j < grid[0].length; j++) {
            ints[0][j] = ints[0][j - 1] + grid[0][j];
        }
        for (int i = 1; i < grid.length; i++) {
            for (int j = 1; j < grid[0].length; j++) {
                ints[i][j] = Math.min(ints[i - 1][j], ints[i][j - 1]) + grid[i][j];
            }
        }
        return ints[grid.length - 1][grid[0].length - 1];
    }
}
